package cn.letlet.util;

import cn.letlet.data.Data;
import cn.letlet.utils.Debug;
import android.os.Message;

public class AiInterface {
	public static final String tag = AiInterface.class.getSimpleName();
	private static AiInterface self;
	public static String bestMove = Data.NULL_BESTMOVE;

	AiInterfaceHelper aiHelper;

	public void startAiThink(String fen,int depth){
		Debug.d("depth:"+depth);
		String cmd3 = "position fen "+fen;
		String cmd4 = "go depth "+depth;
		aiHelper.sendCommand(cmd3);
		aiHelper.sendCommand(cmd4);
	}
	
	public void sendCommand(String cmd){
		aiHelper.sendCommand(cmd);
	}
	
	public void aiMove(String bestMove){
		
	}
	
	public static AiInterface getInstance() {
		if (self == null) {
			self = new AiInterface();
		} 
		return self;

	}
	private AiInterface(){
		//initAiEngine();
	}
	
	public void initAiEngine(){
		aiHelper = new AiInterfaceHelper();
		String libsoPath = "/data/data/cn.letlet.xiangqi/lib/libAi.so";
		aiHelper.setLibsoPath(libsoPath);
		aiHelper.start();
		 while(aiHelper.outputStream==null){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		//String cmd1 = "setoption usemillisec true";
		String cmd2 = "setoption bookfiles libData.so";
		//String cmd3 = "setoption randomness medium";
		
		//aiHelper.sendCommand(cmd1);
		aiHelper.sendCommand(cmd2);
		//aiHelper.sendCommand(cmd3);
	}
	
	public void release(){
		aiHelper.sendCommand("quit");
		aiHelper.release();
		aiHelper=null;
		self = null;
	}

}
